﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Corsat.Library;
using Corsat.Library.DataManager;
using Corsat.Library.Logic;
using Microsoft.Reporting.WebForms;
using Item = Corsat.Library.Item;

namespace Corsat.Web._2013.Reports
{
    public partial class CommiteeForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CurrentAccount = null;
            }
        }

        public Account CurrentAccount
        {
            get
            {
                if (Session["CurrentAccount"] != null)
                {
                    return (Account) Session["CurrentAccount"];
                }
                return null;
            }
            set { Session["CurrentAccount"] = value; }
        }

        protected void BtnSearch_Clicked(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            int corsatId;
            if (int.TryParse(txtCorsatId.Text, out corsatId) && corsatId > 0)
            {
                Item accountItem = Main.Search(SearchOptions.CorsatId, corsatId.ToString()).FirstOrDefault();
                if (accountItem.Id > 0)
                {
                    CurrentAccount = Main.GetAccountById(accountItem.Id);
                    if (CurrentAccount != null && CurrentAccount.AccountId > 0)
                    {
                        txtName.Text = CurrentAccount.ArabicName;
                        if (CurrentAccount.ConferenceBookingDate != null)
                        {
                            txtBookingDate.Text = CurrentAccount.ConferenceBookingDate.Value.ToString("dd/MM/yyy");
                        }
                        else
                        {
                            txtBookingDate.Text = string.Empty;
                        }
                        if (CurrentAccount.ConferenceCompeleteBookingDate != null)
                        {
                            txtCompeleteBookingDate.Text =
                                CurrentAccount.ConferenceCompeleteBookingDate.Value.ToString("dd/MM/yyy");
                        }
                        else
                        {
                            txtCompeleteBookingDate.Text = string.Empty;
                        }


                        if (CurrentAccount.ConferenceStatusId == (int)ConferenceStatus.Booked)
                        {
                            reportViewer.Visible = true;
                            btnBook.Enabled = false;
                            btnCompeleteBooking.Enabled = true;
                            btnCancel.Enabled = true;
                            BindReport();
                        }
                        else if (CurrentAccount.ConferenceStatusId == (int)ConferenceStatus.CompeleteBooking)
                        {
                            reportViewer.Visible = true;
                            btnBook.Enabled = false;
                            btnCompeleteBooking.Enabled = false;
                            btnCancel.Enabled = true;
                            BindReport();
                        }
                        else
                        {
                            reportViewer.Visible = false;
                            btnBook.Enabled = true;
                            btnCompeleteBooking.Enabled = true;
                            btnCancel.Enabled = false;
                        }
                    }
                    else
                    {
                        ResetForm();
                    }
                }
                else
                {
                    ResetForm();
                }
            }
            else
            {
                ResetForm();
            }
        }

        private void ResetForm()
        {
            txtName.Text = string.Empty;
            reportViewer.Visible = false;
            btnBook.Enabled = false;
            btnCompeleteBooking.Enabled = false;
        }

        private void BindReport()
        {
            if (CurrentAccount != null)
            {
                reportViewer.LocalReport.DataSources.Clear();
                reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetAccount", new List<Account> { CurrentAccount }));
            }
            else
            {
                reportViewer.LocalReport.DataSources.Clear();
                reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetAccount", new List<Account>()));
            }
            reportViewer.LocalReport.Refresh();
        }

        protected void btnBook_Click(object sender, EventArgs e)
        {
            if (CurrentAccount != null && CurrentAccount.AccountId > 0)
            {
                Main.UpdateAccountConferenceStatus(CurrentAccount.AccountId, ConferenceStatus.Booked,
                                                        DateTime.Now.Year, DateTime.Now, null);
                Search();
            }
        }
        
        protected void btnCompeleteBooking_Click(object sender, EventArgs e)
        {
            if (CurrentAccount != null && CurrentAccount.AccountId > 0)
            {
                Main.UpdateAccountConferenceStatus(CurrentAccount.AccountId, ConferenceStatus.CompeleteBooking,
                                                        DateTime.Now.Year, null, DateTime.Now);
                Search();
            }
        }
        
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (CurrentAccount != null && CurrentAccount.AccountId > 0)
            {
                Main.UpdateAccountConferenceStatus(CurrentAccount.AccountId, ConferenceStatus.NotBooked,
                                                        DateTime.Now.Year, null, null);
                Search();
            }
        }
    }
}